﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Linq;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.Images;
using Framework.Library.Core.Config;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Client.Modules.Menu
{
    public partial class TeenioTruyen : BaseControl
    {
        public TeenioTruyen()
        {
        }

        public TeenioTruyen(JSection section)
        {
            if(!string.IsNullOrEmpty(section.parameter))
            {
                string[] arrParams = section.parameter.Split('&');
                if (arrParams.Length > 0)
                    menuId = Convert.ToInt32(arrParams[0].Split('=')[1]);
            }
            MENUNAME = section.title;
        }
        public string MENUNAME = string.Empty;

        protected static int menuId = 0;
        protected static int curMenu = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int totalRecord;
                List<TeenioContent> lstContent = ManagerFactory.TeenioContentManager.GetList(0, 3, 2, (int)TeenioContentType.Article, 0, 5, out totalRecord);
                if (lstContent != null)
                {                    
                    rptContent.DataSource = lstContent;
                    rptContent.DataBind();
                }
            }
        }

        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (TeenioContent)e.Item.DataItem;
                var imgAvatar = (Image)e.Item.FindControl("imgAvatar");
                var hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                var lblCreateDate = (Literal)e.Item.FindControl("lblCreateDate");
                var lblTeaser = (Literal)e.Item.FindControl("lblTeaser");
                var hplComment = (HyperLink)e.Item.FindControl("hplComment");

                if (!string.IsNullOrEmpty(info.CONTENT_AVATAR))
                    imgAvatar.ImageUrl = ImagesUtils.GetImage(info.CONTENT_AVATAR, 80, 65);
                hplTitle.Text = info.CONTENT_HEADLINE;
                hplTitle.NavigateUrl = cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID);
                lblCreateDate.Text = DateTimeHelper.formatDateTime(info.CONTENT_MODIFYDDATE);
                lblTeaser.Text = ConvertUtility.ToSubString(info.CONTENT_TEASER, 260);
                hplComment.NavigateUrl = cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID);
            }
        }
    }
}